package com.zhou3;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.zhou.MyAccessDataBase;
import com.zhou.Spider;

public class MyForm extends JFrame {

	public String proxyIpAndPort = "210.5.149.43:8090";// 最好用的代理IP

	public boolean isStopDownload = false;

	public JLabel outputLabel = new JLabel("输出文件：");
	public JTextField outputField = new JTextField("data.accdb");
	public JButton okJButton = new JButton("确定");

	public JLabel saveTableNameJLabel = new JLabel("存储表名：");
	public JTextField saveTableName = new JTextField("表一");

	public JLabel proxyJLabel = new JLabel("代理：");
	public JTextField proxyIPAndPort = new JTextField("122.72.18.34:80");
	public JCheckBox proxyJCheckBox = new JCheckBox("启用代理");

	public JLabel urlJLabel = new JLabel("登录网址:");
	public JTextField urlField = new JTextField(
			"http://www.pss-system.gov.cn/sipopublicsearch/portal/uilogin-forwardLogin.shtml");
	public JButton openLoginUrlButton = new JButton("打开网页");

	public JLabel userNameLabel = new JLabel("用户名:");
	public JTextField userNameField = new JTextField("muronghuoxiao3");

	public JLabel passwordLabel = new JLabel("密码:");
	public JPasswordField passwordField = new JPasswordField("19940203XIN");

	public JLabel captchaJLabel = new JLabel("验证码:");
	public JTextField captchaField = new JTextField();
	public JButton loginButton = new JButton("登录");

	public JLabel loginState1Label = new JLabel("登录状态:");
	public JLabel loginState2Label = new JLabel("未登录");

	public JCheckBox noVisibleBox = new JCheckBox("不显示浏览器");

	public JLabel searchLabel = new JLabel("检索式:");
	public JTextField searchField = new JTextField("申请日=19850910:20171227");
	public JButton searchButton = new JButton("检索");

	public JLabel searchResultLabel = new JLabel("总共页数:");
	public JLabel sumPageLabel = new JLabel("0");

	public JLabel startPageLabel = new JLabel("起始页:");
	public JTextField startPageField = new JTextField("1");
	public JLabel nowDownloadPageJLabel = new JLabel("0");
	public JButton readDownloadPageFromDB = new JButton("从数据库读取");

	public JLabel endPageLabel = new JLabel("终止页:");
	public JTextField endPageField = new JTextField("1");

	public JButton downloadButton = new JButton("开始下载");

	public JLabel downloadedLabel = new JLabel("下载进度:");
	public JProgressBar downloadProgressBar = new JProgressBar();
	public JLabel downloadedJLabel = new JLabel("0/0");

	public JLabel dealFileName = new JLabel("要处理的文件名:");
	public JTextField dealFileNameField = new JTextField("");

	public JLabel sourceTableName = new JLabel("源表名:");
	public JTextField sourceTableNameField = new JTextField("");

	public JLabel destTableName = new JLabel("目的表名:");
	public JTextField destTableNameField = new JTextField("");
	public JButton startDeal = new JButton("开始转存");

	public JButton resetButton = new JButton("重来");

	public MyForm() {
		super();

		// 初始化面板1
		// initPanel1();
		initPanel2();

		// 添加事件
		addEvent();

		this.setTitle("药物专利检索");
		this.setSize(600, 600);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocation(300, 20);
		this.setVisible(true);
	}

	/**
	 * 初始化面板2
	 */
	private void initPanel2() {

		Font font = new Font("宋体", Font.PLAIN, 50);
		UIManager.put("JButton.font", font);

		JPanel mainJPanel = new JPanel();
		mainJPanel.setLayout(new BorderLayout(20, 10));
		this.add(mainJPanel);

		JPanel leftJPanel = new JPanel();
		JPanel centerJPanel = new JPanel();
		JPanel rightJPanel = new JPanel();

		mainJPanel.setBorder(BorderFactory
				.createLineBorder(Color.LIGHT_GRAY, 5));

		mainJPanel.add(leftJPanel, BorderLayout.WEST);
		mainJPanel.add(centerJPanel, BorderLayout.CENTER);
		mainJPanel.add(rightJPanel, BorderLayout.EAST);

		leftJPanel.setLayout(new GridLayout(0, 1, 5, 10));
		centerJPanel.setLayout(new GridLayout(0, 1, 5, 10));
		rightJPanel.setLayout(new GridLayout(0, 1, 5, 10));

		leftJPanel.add(outputLabel);
		centerJPanel.add(outputField);
		rightJPanel.add(okJButton);

		leftJPanel.add(saveTableNameJLabel);
		centerJPanel.add(saveTableName);
		rightJPanel.add(new JLabel());

		leftJPanel.add(proxyJLabel);
		centerJPanel.add(proxyIPAndPort);
		rightJPanel.add(proxyJCheckBox);

		leftJPanel.add(urlJLabel);
		centerJPanel.add(urlField);
		rightJPanel.add(openLoginUrlButton);

		leftJPanel.add(userNameLabel);
		centerJPanel.add(userNameField);
		rightJPanel.add(new JLabel());

		leftJPanel.add(passwordLabel);
		centerJPanel.add(passwordField);
		rightJPanel.add(new JLabel());

		leftJPanel.add(captchaJLabel);
		centerJPanel.add(captchaField);
		rightJPanel.add(loginButton);

		leftJPanel.add(loginState1Label);
		centerJPanel.add(loginState2Label);
		rightJPanel.add(noVisibleBox);

		leftJPanel.add(searchLabel);
		centerJPanel.add(searchField);
		rightJPanel.add(searchButton);

		leftJPanel.add(searchResultLabel);
		centerJPanel.add(sumPageLabel);
		rightJPanel.add(new JLabel());

		leftJPanel.add(startPageLabel);
		centerJPanel.add(startPageField);
		rightJPanel.add(nowDownloadPageJLabel);

		leftJPanel.add(endPageLabel);
		centerJPanel.add(endPageField);
		rightJPanel.add(downloadButton);

		leftJPanel.add(downloadedLabel);
		centerJPanel.add(downloadProgressBar);
		rightJPanel.add(downloadedJLabel);

		leftJPanel.add(dealFileName);
		centerJPanel.add(dealFileNameField);
		rightJPanel.add(new JLabel());

		leftJPanel.add(sourceTableName);
		centerJPanel.add(sourceTableNameField);
		rightJPanel.add(new JLabel());

		leftJPanel.add(destTableName);
		centerJPanel.add(destTableNameField);
		rightJPanel.add(startDeal);

		mainJPanel.add(resetButton, BorderLayout.SOUTH);
	}

	/**
	 * 添加事件
	 */
	private void addEvent() {
		proxyJCheckBox.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				if (proxyJCheckBox.isSelected()) {

				} else {

				}
			}
		});
		openLoginUrlButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (Spider.driver != null) {
					Spider.driver.quit();
				}
				Spider.login(urlField.getText(), userNameField.getText(),
						String.valueOf(passwordField.getPassword()));
			}
		});
		loginButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				boolean b = Spider.inputCaptcha(captchaField.getText());
				if (b) {
					loginState2Label.setText("登录成功");
					loginState2Label.setForeground(Color.red);
				} else {
					loginState2Label.setText("登录失败");
					JOptionPane.showMessageDialog(null, "登录失败!");
					loginState2Label.setForeground(Color.green);
				}
			}
		});
		resetButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (Spider.driver != null) {
					if (Spider.driver != null) {
						Spider.driver.quit();
					}
					loginState2Label.setText("未登录");
					loginState2Label.setForeground(Color.black);
					sumPageLabel.setForeground(Color.black);
					isStopDownload = true;
					downloadButton.setEnabled(true);
				}
			}
		});

		searchButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Spider.advanceSearch(noVisibleBox.isSelected(),
						searchField.getText());

				int downloadPage = MyAccessDataBase.getDownloadPage();

				if (downloadPage < 0) {
					JOptionPane.showMessageDialog(null, "数据库里无此信息，请手动输入【起始页】!");
				} else {
					startPageField.setText((downloadPage + 1) + "");
				}
			}
		});
		readDownloadPageFromDB.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int downloadPage = MyAccessDataBase.getDownloadPage();

				if (downloadPage < 0) {
					JOptionPane.showMessageDialog(null, "数据库里无此信息!");
				} else {
					startPageField.setText((downloadPage + 1) + "");
				}
			}
		});
		okJButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MyAccessDataBase.dbpath = outputField.getText();

				File file = new File(outputField.getText());

				if (file.exists()) {
					JOptionPane.showMessageDialog(null, "成功更改");
				} else {
					JOptionPane.showMessageDialog(null, "输出路径的不正确，请更换！");
				}
			}
		});
		downloadButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				isStopDownload = false;

				File file = new File(outputField.getText());

				if (file.exists()) {
					Spider.download(startPageField.getText(),
							endPageField.getText());
				} else {
					JOptionPane.showMessageDialog(null, "输出路径的不正确，请更换！");
				}
			}
		});
		startDeal.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {				
				new Thread(new Runnable() {					
					public void run() {	
						startDeal.setEnabled(false);
						MyAccessDataBase.startTransferData(dealFileNameField.getText(),
								sourceTableNameField.getText(),
								destTableNameField.getText());
						startDeal.setEnabled(true);
						JOptionPane.showMessageDialog(null, "数据转存成功！");
					}
				}).start();
			}
		});

		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {

				isStopDownload = true;

				if (Spider.driver != null) {
					Spider.driver.quit();
				}
			}
		});
	}

	/**
	 * 初始化面板1
	 */
	private void initPanel1() {
		this.setLayout(null);

		this.add(urlJLabel);
		this.add(urlField);
		this.add(openLoginUrlButton);

		this.add(userNameLabel);
		this.add(userNameField);

		this.add(passwordLabel);
		this.add(passwordField);

		this.add(captchaJLabel);
		this.add(captchaField);
		this.add(loginButton);

		this.add(loginState1Label);
		this.add(loginState2Label);
		this.add(noVisibleBox);

		/*
		 * this.add(keywordLabel); this.add(keywordJTextField);
		 * 
		 * this.add(tableNameJLabel); this.add(tableNameJTextField);
		 * 
		 * this.add(fieldNameJLabel); this.add(fieldNameJTextField);
		 * this.add(keywordJButton);
		 * 
		 * this.add(readResultJLabel1); this.add(readResultJLabel2);
		 */

		this.add(searchLabel);
		this.add(searchField);
		this.add(searchButton);

		this.add(searchResultLabel);
		this.add(sumPageLabel);

		this.add(startPageLabel);
		this.add(startPageField);
		this.add(readDownloadPageFromDB);
		this.add(endPageLabel);
		this.add(endPageField);
		this.add(downloadButton);

		this.add(downloadedLabel);
		this.add(downloadProgressBar);
		this.add(downloadedJLabel);

		this.add(outputLabel);
		this.add(outputField);

		this.add(resetButton);

		urlJLabel.setBounds(10, 10, 60, 20);
		urlField.setBounds(80, 10, 200, 20);
		openLoginUrlButton.setBounds(290, 10, 100, 20);

		userNameLabel.setBounds(10, 40, 60, 20);
		userNameField.setBounds(80, 40, 200, 20);

		passwordLabel.setBounds(10, 70, 60, 20);
		passwordField.setBounds(80, 70, 200, 20);

		captchaJLabel.setBounds(10, 100, 60, 20);
		captchaField.setBounds(80, 100, 200, 20);
		loginButton.setBounds(290, 100, 100, 20);

		loginState1Label.setBounds(10, 130, 60, 20);
		loginState2Label.setBounds(80, 130, 200, 20);
		noVisibleBox.setBounds(290, 130, 120, 20);

		/*
		 * keywordLabel.setBounds(10, 160, 60, 20);
		 * keywordJTextField.setBounds(80, 160, 200, 20);
		 * 
		 * tableNameJLabel.setBounds(10, 190, 60, 20);
		 * tableNameJTextField.setBounds(80, 190, 200, 20);
		 * 
		 * fieldNameJLabel.setBounds(10, 220, 60, 20);
		 * fieldNameJTextField.setBounds(80, 220, 200, 20);
		 * keywordJButton.setBounds(290, 220, 100, 20);
		 * 
		 * readResultJLabel1.setBounds(10, 250, 60, 20);
		 * readResultJLabel2.setBounds(80, 250, 200, 20);
		 */

		searchLabel.setBounds(10, 280, 60, 20);
		searchField.setBounds(80, 280, 200, 20);
		searchButton.setBounds(290, 280, 100, 20);

		searchResultLabel.setBounds(10, 310, 60, 20);
		sumPageLabel.setBounds(80, 310, 200, 20);

		startPageLabel.setBounds(10, 340, 60, 20);
		startPageField.setBounds(80, 340, 200, 20);
		readDownloadPageFromDB.setBounds(290, 340, 100, 20);

		endPageLabel.setBounds(10, 370, 60, 20);
		endPageField.setBounds(80, 370, 200, 20);
		downloadButton.setBounds(290, 370, 100, 20);

		downloadedLabel.setBounds(10, 400, 60, 20);
		downloadProgressBar.setBounds(80, 400, 200, 20);
		downloadedJLabel.setBounds(290, 400, 200, 20);

		downloadProgressBar.setForeground(Color.green);

		outputLabel.setBounds(10, 430, 60, 20);
		outputField.setBounds(80, 430, 200, 20);

		resetButton.setBounds(10, 460, 380, 40);
	}

	public JLabel getUrlJLabel() {
		return urlJLabel;
	}

	public void setUrlJLabel(JLabel urlJLabel) {
		this.urlJLabel = urlJLabel;
	}

	public JTextField getUrlField() {
		return urlField;
	}

	public void setUrlField(JTextField urlField) {
		this.urlField = urlField;
	}

	public JButton getOpenLoginUrlButton() {
		return openLoginUrlButton;
	}

	public void setOpenLoginUrlButton(JButton openLoginUrlButton) {
		this.openLoginUrlButton = openLoginUrlButton;
	}

	public JLabel getUserNameLabel() {
		return userNameLabel;
	}

	public void setUserNameLabel(JLabel userNameLabel) {
		this.userNameLabel = userNameLabel;
	}

	public JTextField getUserNameField() {
		return userNameField;
	}

	public void setUserNameField(JTextField userNameField) {
		this.userNameField = userNameField;
	}

	public JLabel getPasswordLabel() {
		return passwordLabel;
	}

	public void setPasswordLabel(JLabel passwordLabel) {
		this.passwordLabel = passwordLabel;
	}

	public JPasswordField getPasswordField() {
		return passwordField;
	}

	public void setPasswordField(JPasswordField passwordField) {
		this.passwordField = passwordField;
	}

	public JLabel getCaptchaJLabel() {
		return captchaJLabel;
	}

	public void setCaptchaJLabel(JLabel captchaJLabel) {
		this.captchaJLabel = captchaJLabel;
	}

	public JTextField getCaptchaField() {
		return captchaField;
	}

	public void setCaptchaField(JTextField captchaField) {
		this.captchaField = captchaField;
	}

	public JButton getLoginButton() {
		return loginButton;
	}

	public void setLoginButton(JButton loginButton) {
		this.loginButton = loginButton;
	}

	public JLabel getLoginState1Label() {
		return loginState1Label;
	}

	public void setLoginState1Label(JLabel loginState1Label) {
		this.loginState1Label = loginState1Label;
	}

	public JLabel getLoginState2Label() {
		return loginState2Label;
	}

	public void setLoginState2Label(JLabel loginState2Label) {
		this.loginState2Label = loginState2Label;
	}

	public JCheckBox getNoVisibleBox() {
		return noVisibleBox;
	}

	public void setNoVisibleBox(JCheckBox noVisibleBox) {
		this.noVisibleBox = noVisibleBox;
	}

	public JLabel getSearchLabel() {
		return searchLabel;
	}

	public void setSearchLabel(JLabel searchLabel) {
		this.searchLabel = searchLabel;
	}

	public JTextField getSearchField() {
		return searchField;
	}

	public void setSearchField(JTextField searchField) {
		this.searchField = searchField;
	}

	public JButton getSearchButton() {
		return searchButton;
	}

	public void setSearchButton(JButton searchButton) {
		this.searchButton = searchButton;
	}

	public JLabel getSearchResultLabel() {
		return searchResultLabel;
	}

	public void setSearchResultLabel(JLabel searchResultLabel) {
		this.searchResultLabel = searchResultLabel;
	}

	public JLabel getSumPageLabel() {
		return sumPageLabel;
	}

	public void setSumPageLabel(JLabel sumPageLabel) {
		this.sumPageLabel = sumPageLabel;
	}

	public JLabel getStartPageLabel() {
		return startPageLabel;
	}

	public void setStartPageLabel(JLabel startPageLabel) {
		this.startPageLabel = startPageLabel;
	}

	public JTextField getStartPageField() {
		return startPageField;
	}

	public void setStartPageField(JTextField startPageField) {
		this.startPageField = startPageField;
	}

	public JLabel getEndPageLabel() {
		return endPageLabel;
	}

	public void setEndPageLabel(JLabel endPageLabel) {
		this.endPageLabel = endPageLabel;
	}

	public JTextField getEndPageField() {
		return endPageField;
	}

	public void setEndPageField(JTextField endPageField) {
		this.endPageField = endPageField;
	}

	public JButton getDownloadButton() {
		return downloadButton;
	}

	public void setDownloadButton(JButton downloadButton) {
		this.downloadButton = downloadButton;
	}

	public JLabel getDownloadedLabel() {
		return downloadedLabel;
	}

	public void setDownloadedLabel(JLabel downloadedLabel) {
		this.downloadedLabel = downloadedLabel;
	}

	public JProgressBar getDownloadProgressBar() {
		return downloadProgressBar;
	}

	public void setDownloadProgressBar(JProgressBar downloadProgressBar) {
		this.downloadProgressBar = downloadProgressBar;
	}

	public JLabel getDownloadedJLabel() {
		return downloadedJLabel;
	}

	public void setDownloadedJLabel(JLabel downloadedJLabel) {
		this.downloadedJLabel = downloadedJLabel;
	}

	public JButton getResetButton() {
		return resetButton;
	}

	public void setResetButton(JButton resetButton) {
		this.resetButton = resetButton;
	}
}
